#!/usr/bin/env python
# -*- coding: utf-8 -*-
import db_util as db

def create_table():
    conn = db.get_connector()
    sql_str = '''
                CREATE TABLE `hot_like` (
                `lid` int(10) NOT NULL AUTO_INCREMENT,
                `user` varchar(20) NOT NULL, 
                `topic` varchar(50) NOT NULL,
                `num` int(10) NOT NULL,
                PRIMARY KEY (`lid`)
                )
                '''
    db.op_table(conn, sql_str)
    db.close_connector(conn)

def drop_table():
    conn = db.get_connector()
    sql_str = '''
                DROP TABLE `hot_like`
                '''
    db.op_table(conn, sql_str)
    db.close_connector(conn)
    
def insert(sql_data):
    conn = db.get_connector()
    sql_str = ("INSERT INTO hot_like (user, topic, num) VALUES (%s, %s, %s)")
#     sql_data = ('20151001', '00', 'TOPIC1', 50)
    db.insert(conn, sql_str, sql_data)
    db.close_connector(conn)
    
def query():
    conn = db.get_connector()
    sql_str = ("SELECT user, topic, num FROM hot_like ORDER BY num DESC LIMIT 30")
#     sql_data = ('20151001', 'TOPIC1')
    cursor = db.query(conn, sql_str)
    result = []
    for (user, topic, num) in cursor:
        result.append([user, topic, num])
    db.close_connector(conn)
    return result

def init_data():
    input_file ='/home/ivy/git/Social/Social/resource/like/like_1000.txt'
    with open(input_file, 'r+') as f:
        counter = 0
        for line in f:
            if counter == 100:
                break
            arr = line.strip().split('\t')
            sql_data = (arr[0], arr[1], arr[2])  
            insert(sql_data) 
            counter += 1
    f.close()

def main():
    print ''
#     drop_table()
#     create_table()
#     init_data()
    
    
if __name__ == '__main__':
    main()
        